<?php
namespace app\api\controller\xilumarket;

use think\Exception;
use function fast\array_get;
use app\common\controller\Api;
use app\common\model\xilumarket\UserFocus;
use app\common\model\xilumarket\FooterView;
use app\common\model\xilumarket\TopicForum;
use app\common\model\xilumarket\TopicBanUser;
use app\common\model\xilumarket\UserFavourite;
use app\common\model\xilumarket\UserCollection;

class Forum extends Api
{
    protected $noNeedLogin = ['focus_lists','hot_lists','lists','detail'];
    protected $noNeedRight = ['*'];


    /**
     * 热门帖子
     */
    public function hot_lists(){
        $params = $this->request->param('');
        $result = TopicForum::recmmendLists($params,$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 帖子列表
     */
    public function lists(){
        $params = $this->request->param('');
        $result = TopicForum::lists($params,$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 关注用户帖子列表
     */
    public function focus_lists(){
        $params = $this->request->param('');
        $result = TopicForum::focusLists($params,$this->auth->id);
        $this->success('查询成功', $result);
    }

    /**
     * 帖子详情
     */
    public function detail(){
        $params = $this->request->param('');
        $forum_id = array_get($params,'forum_id');
        if(!$forum_id){
            $this->error('非法参数');
        }
        $result = TopicForum::detail($forum_id,$this->auth->id);
        #足迹
        if($this->auth->isLogin()){
            try {
                FooterView::viewFooter($this->auth->id,$forum_id,FooterView::TYPE_FORUM);
            }catch (Exception $e){
                //$this->error($e->getMessage());
            }
        }

        $this->success('查询成功', $result);
    }

    /**
     * 新增帖子
     */
    public function add_forum(){
        $params = $this->request->param('');
        $banUser = TopicBanUser::where(['topic_id'=>$params['topic_id'], 'user_id' => $this->auth->id,'expir_time'=>['>', time()]])->find();
        if($banUser) {
            $this->error('您已被禁言');
        }
        try {
            $ret = TopicForum::add($params,$this->auth->id);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
        $this->success('发表成功',$ret);
    }

    /**
     * 我的帖子删除
     */
    public function del_forum(){
        $params = $this->request->param('');
        try {
            $result = TopicForum::delForum($params,$this->auth->id);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

        $this->success('删除成功');
    }

    /**
     * @ApiTitile (收藏&取消收藏)
     * @ApiSummary (收藏&取消收藏)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilumarket.forum/toggle_collection)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     * @ApiParams (name=forum_id, type=int, require=true, description="id")
     */
    public function toggle_collection() {
        $forum_id = $this->request->post('forum_id');
        $result = UserCollection::toggleCollection($this->auth->id, $forum_id, UserCollection::TYPE_FORUM);
        $row = TopicForum::detail($forum_id,$this->auth->id);
        $this->success('操作成功',$row);
    }

    /**
     * @ApiTitile (点赞&取消点赞)
     * @ApiSummary (点赞&取消点赞)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilumarket.forum/toggle_fav)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     * @ApiParams (name=forum_id, type=int, require=true, description="id")
     */
    public function toggle_fav() {
        $forum_id = $this->request->post('forum_id');
        $user_id = $this->auth->id;
        $result = UserFavourite::toggleFav($this->auth->id, $forum_id, UserFavourite::TYPE_FORUM);
        $row = TopicForum::detail($forum_id,$user_id);
        $this->success('操作成功',$row);
    }

    /**
     * 关注和取关
     */
    public function toggle_focus(){
        $user_id = $this->request->post('user_id');
        if($user_id == $this->auth->id){
            $this->error('不可关注自己');
        }
        try {
            $result = UserFocus::toggleFocus($this->auth->id, $user_id);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }

        $this->success('操作成功',['is_focus_count'=>$result ? 1 : 0]);
    }


    /**
     * 我的收藏列表
     */
    public function user_collection(){
        $params = $this->request->param('');
        $this->success('查询成功',UserCollection::ForumLists($params,$this->auth->id));
    }

    /**
     * 删除
     */
    public function toggle_del(){
        $forum_id = $this->request->post('forum_id');
        $forum = $forum_id? TopicForum::get($forum_id):null;
        if(!$forum){
            $this->error("未找到帖子");
        }
        if($forum->user_id != $this->auth->id){
            $this->error("没有删除权限");
        }
        $forum->delete();
        $this->success('删除成功', $forum);
    }

    /**
     * 我的关注列表
     */
    public function user_focus(){
        $params = $this->request->param('');
        $this->success('查询成功',UserFocus::focusLists($params,$this->auth->id));
    }

    /**
     * 我的粉丝列表
     */
    public function user_fans(){
        $params = $this->request->param('');
        $this->success('查询成功',UserFocus::fansLists($params,$this->auth->id));
    }

}